package com.ihd.ihardware.view.enter.model;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ihd.ihardware.api.HttpClient;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.pojo.BannerRes;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.pojo.FileRes;
import com.ihd.ihardware.pojo.LoginRes;
import com.ihd.ihardware.pojo.UpdateRes;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.reactivestreams.Publisher;

import java.io.File;
import java.util.List;

import cn.wowjoy.commonlibrary.app.Constans;
import cn.wowjoy.commonlibrary.http.BaseSubscriber;
import cn.wowjoy.commonlibrary.utils.FileUtils;
import cn.wowjoy.commonlibrary.utils.RxSchedulerTransformer;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.viewmodel.BaseRepository;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class LoginRepository extends BaseRepository {

    public void login(String username, String password) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("mobile", username);
        mParam.addProperty("password", password);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().login(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<LoginRes>(Constans.LOGIN) {
                }));
    }

    public void wxLogin(String code) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("code", code);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().wxLogin(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<LoginRes>(Constans.LOGIN) {
                }));
    }

    public void sendVerifySms(String phone, String type) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("mobile", phone);
        mParam.addProperty("type", type);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().sendVerifySms(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(Constans.CODES) {
                }));
    }

    public void regist(String phone, String password, String smsCode) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("mobile", phone);
        mParam.addProperty("password", password);
        mParam.addProperty("smsCode", smsCode);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().regist(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<LoginRes>(Constans.REGIST) {
                }));
    }

    public void resetPwd(String phone, String password, String smsCode) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("mobile", phone);
        mParam.addProperty("password", password);
        mParam.addProperty("smsCode", smsCode);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().resetPwd(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<LoginRes>(Constans.RESET) {
                }));
    }

    public void updateHead(File file) {
        MultipartBody.Part requestBody = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
        addDisposable(HttpClient.getService().uploadToOss(requestBody)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<FileRes>(AppConstans.UPDATE) {
                }));
    }

    public void updateInfo(String avatar, String nickName, String sex, String birthday, String height, String weight) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("avatar", avatar);
        mParam.addProperty("nickName", nickName);
        mParam.addProperty("sex", sex);
        mParam.addProperty("birthday", birthday);
        mParam.addProperty("height", height);
        mParam.addProperty("weight", weight);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().userInfo(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(AppConstans.UPDATEINFO) {
                }));
    }

    public void dream(String weight) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("weight", weight);
        mParam.addProperty("weightPeriod", "0");
        mParam.addProperty("stepNumber", "0");
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().dream(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(AppConstans.DREAM) {
                }));
    }

    public void getUserInfo() {
        addDisposable(HttpClient.getService().deviceList(1, 100)
                .flatMap(new Function<DeviceListRes, Publisher<UserInfoRes>>() {
                    @Override
                    public Publisher<UserInfoRes> apply(DeviceListRes deviceListRes) throws Exception {
                        if (deviceListRes.getData().getList().size() == 0)
                            SPUtils.putString(AppConstans.DEVICES, "");
                        else
                            SPUtils.putString(AppConstans.DEVICES, new Gson().toJson(deviceListRes.getData().getList(), new TypeToken<List<DeviceListRes.DataBean.ListBean>>() {
                            }.getType()));
                        return HttpClient.getService().getUserInfo();
                    }
                })
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<UserInfoRes>(AppConstans.INFO) {
                }));
    }

    public void getBanner() {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("type", "1");
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().getBanner(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<BannerRes>(AppConstans.BANNER) {
                }));
    }

    public void update() {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("platformType", "1");
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().getLatestVersion(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<UpdateRes>(AppConstans.MUPGRADE) {
                }));
    }

    public void download(UpdateRes updateInfo) {
        Disposable disposable = HttpClient.getService().downloadapk(updateInfo.getData().getVersionUrl())
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribeWith(new DisposableObserver<ResponseBody>() {
                    @Override
                    public void onNext(ResponseBody responseBody) {
                        if (FileUtils.writeResponseBodyToDisk(responseBody, AppConstans.UPDATE_PATH)) {
                            LiveEventBus.get().with(AppConstans.M_DOWNLOAD_APK).post(updateInfo);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
        addDisposable(disposable);
    }
}
